package com.wefans.wefansapp.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.BenefitUserData;
import com.wefans.wefansapp.common.tool.data.BenefitUserResponseData;
import com.wefans.wefansapp.common.tool.data.BenefitUserResponseWinData;
import com.wefans.wefansapp.common.tool.data.BenefitVoteData;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetImage;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;

public class BenefitVoteActivity extends BaseActivity implements IOnNetworkListener{
	
	
	private BenefitVoteData mBenefitVoteDate = new BenefitVoteData();
	private MFNetworkManager m_oNetworManager = null;
    private MFDataManager    m_oDataManager   = null;
	private DrawerLayout   m_DrawerLayout  = null;
    private FrameLayout    m_DrawerView    = null;
    private ImageView	   m_CancelBtn	   = null;
    private TextView	   m_TitleView	   = null;
    private TextView	   m_UpBtn		   = null;
    private TextView	   m_DownBtn	   = null;
    private TextView	   m_LeftBtn	   = null;
    private TextView	   m_RightBtn	   = null;
    private EditText	   m_CrntUser	   = null;
    private TextView	   m_TotalUser	   = null;
    private TextView	   m_LegacyText	   = null;
    private TextView	   m_BenefitCancel = null;
    private TextView	   m_BenefitComplete = null;
    private int 		   m_CrntUsr	   = 0;
    private int 		   m_MaxUsr	   = 0;
    private String		   m_tmpString	   = null;
    private String         m_Title		   = null;
    private String         m_EventIdx	   = null;
    private Toast toastVote;
    private Intent it;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_benefit_vote);
		it = getIntent();
		m_Title = it.getStringExtra("title");
		m_EventIdx = it.getStringExtra("event_idx");
		initialize();
        installEvent();
		
		setTitle("BenefitVote");
	}
	
	
	@Override
    public void onClick(View v)
    {
        MFDebug.d(">> onClick()");
        
        switch( v.getId() ) {
        case R.id.TV_BENEFIT_VOTE_UP:
        	if(m_CrntUsr == m_MaxUsr){// max취득하면 변경
        		m_CrntUser.setText(String.valueOf(m_MaxUsr));
        		m_CrntUsr = m_MaxUsr; //max고정
        	}else {
        		m_CrntUsr=m_CrntUsr+1;
        		m_CrntUser.setText(String.valueOf(m_CrntUsr));
        	}
        	break;
        case R.id.TV_BENEFIT_VOTE_DOWN:
        	if(m_CrntUsr == 0){
        		m_CrntUser.setText("0");
        		m_CrntUsr = 0;
        	}else {
        		m_CrntUsr=m_CrntUsr-1;
        		m_CrntUser.setText(String.valueOf(m_CrntUsr));
        	}
        	break;
        case R.id.TV_BENEFIT_VOTE_LEFT:
        	m_CrntUser.setText("0");
        	m_CrntUsr = 0;
        	break;       	
        case R.id.TV_BENEFIT_VOTE_RIGHT:
        	//max정보 취득하여 표시
        	m_CrntUser.setText(String.valueOf(m_MaxUsr));
        	m_CrntUsr = m_MaxUsr;
        	break;        	
        case R.id.IV_BENEFIT_VOTE_X:
	        MFDebug.d("++ v.getId() : " + "IV_BENEFIT_VOTE_X");
			it = new Intent(getApplicationContext(), BenefitActivity.class);
			startActivityForResult(it,v.getId());
        	break;       	
        case R.id.TV_BENEFIT_VOTE_CANCEL:
	        MFDebug.d("++ v.getId() : " + "TV_BENEFIT_VOTE_CANCEL");
			it = new Intent(getApplicationContext(), BenefitActivity.class);
			startActivityForResult(it,v.getId());
        	break;        	
        case R.id.TV_BENEFIT_VOTE_SEND:
        	MFDebug.d("++ v.getId() : " + "TV_BENEFIT_DETAIL_QUICK_SEND");
	        MFDataManager mem_idx = MFDataManager.getInstance(this);
	        m_oNetworManager.requestBenfitVote(mem_idx.getMebmerIdx(),m_EventIdx,"1",this);
        	break;        	
        default:
        	break;
        }
    }
	
    private void initialize()
    {
        MFDebug.d(">> initialize()");
        m_oNetworManager = MFNetworkManager.getInstance(this);
        m_oDataManager   = MFDataManager.getInstance(this);
        //Contents layout adding
        try{
        	//m_MaxUsr = Integer.parseInt(m_oDataManager.getUserInfo().getTicketcnt());
        	m_MaxUsr = 3570;
        }catch (Exception e){
        	
        }

        FrameLayout containerContents = (FrameLayout) findViewById(R.id.FL_BENEFIT_VOTE_CONTAINER);
        addContentsLayout(containerContents, R.layout.layout_benefit_vote);
        
        //Drawer menu adding
        m_DrawerView = (FrameLayout) findViewById(R.id.FL_BENEFIT_VOTE_DRAWER_LAYOUT);
        addDrawerLayout(m_DrawerView);
        
        //set drawer layout (root)
        m_DrawerLayout = (DrawerLayout) findViewById(R.id.DL_BENEFIT_VOTE_NAVIGATION_LAYOUT);
    }
    
    private void installEvent()
    {
        MFDebug.d(">> installEvent()");
        m_CancelBtn			= (ImageView) findViewById(R.id.IV_BENEFIT_VOTE_X);
        m_TitleView = (TextView) findViewById(R.id.TV_BENEFIT_DRAWER_MY_INFO_NICNAME);
        m_UpBtn			= (TextView) findViewById(R.id.TV_BENEFIT_VOTE_UP);
        m_DownBtn	= (TextView) findViewById(R.id.TV_BENEFIT_VOTE_DOWN);
        m_LeftBtn 	= (TextView) findViewById(R.id.TV_BENEFIT_VOTE_LEFT);
        m_RightBtn 	= (TextView) findViewById(R.id.TV_BENEFIT_VOTE_RIGHT);
        m_CrntUser 	= (EditText) findViewById(R.id.ET_BENEFIT_VOTE_CNT);
        m_TotalUser 	= (TextView) findViewById(R.id.TV_BENEFIT_VOTE_TOTAL_CNT);
        m_LegacyText 	= (TextView) findViewById(R.id.TV_BENEFIT_VOTE_TEXT);
        m_BenefitCancel 	= (TextView) findViewById(R.id.TV_BENEFIT_VOTE_CANCEL);
        m_BenefitComplete 	= (TextView) findViewById(R.id.TV_BENEFIT_VOTE_SEND);

        
        //todo detail 정보 취득하여 입력하는 것으로 수정
        m_TitleView.setText(m_Title);
        //서브타이틀은 데이터 파싱을 통해 구해 표시
        m_CrntUser.setText("5");
        //m_tmpString = m_CrntUser.getText();
        m_CrntUsr = Integer.parseInt("5");
        m_TotalUser.setText("3570");
        m_LegacyText.setText("응모 1회당 응모권 1장 차감");
         
		
        findViewById(R.id.IV_BENEFIT_VOTE_X).setOnClickListener(this);
        findViewById(R.id.TV_BENEFIT_VOTE_UP).setOnClickListener(this);
		findViewById(R.id.TV_BENEFIT_VOTE_DOWN).setOnClickListener(this);
		findViewById(R.id.TV_BENEFIT_VOTE_LEFT).setOnClickListener(this);
		findViewById(R.id.TV_BENEFIT_VOTE_RIGHT).setOnClickListener(this);
		findViewById(R.id.TV_BENEFIT_VOTE_CANCEL).setOnClickListener(this);
		findViewById(R.id.TV_BENEFIT_VOTE_SEND).setOnClickListener(this);
        
    }


    @Override
	public void onNetMessage(int resultCode, NetMessage message) {
	    MFDebug.d(">> onNetMessage()");
	    MFDebug.d("++ resultCode : " + resultCode);
	    MFDebug.d("++ message : " + message);
	    
	    try {
	        //서버 여동 데이터
	        if( message instanceof NetJsonData ) {
	            long magicNum = ((NetJsonData) message).getMagicNum();
	            String json = ((NetJsonData) message).getResponseJson();
	            
	            if( json == null ) {
	                throw new Exception("invalid json data");
	            }
	            
	            Gson gson = new Gson();
	            
	            if( magicNum == MFNetworkManager.REQUEST_BENEFIT_VOTE ) {
                	mBenefitVoteDate = (BenefitVoteData)gson.fromJson(json, BenefitVoteData.class);
                    if( mBenefitVoteDate == null ) {
                        throw new Exception("Gson parsing error");
                    }
                    if( mBenefitVoteDate.getResultcode().equals("1") ){
                    	toastVote = Toast.makeText(this, getString(R.string.benefit_vote_success), Toast.LENGTH_LONG);
                    }else{
                    	toastVote = Toast.makeText(this, mBenefitVoteDate.getErrorcode() + ":" + getString(R.string.benefit_vote_fail), Toast.LENGTH_LONG);
                    }
                    toastVote.show();
                }
	        }
	        //이미지 데이터
	        else if( message instanceof NetImage ) {
	            
	        }
	        else {
	            //ignore...
	        }
	    } catch(Exception e) {
	        MFDebug.e(e);
	    }
	}


}